#include "libnfile.h"


long long nfile_get_size(const char *path)
{
  struct stat file_info;

  lstat(path, &file_info);
  return file_info.st_size;
  
}

char    *nfile_get_size_s(const char *path, char *buff, int buff_size)
{
  long long size;
  size_t size_msg;
  
  
  buff = memset(buff, 0, buff_size);
  
  size = nfile_get_size(path);
  if(size / 1073741824 > 0)
    size_msg = sprintf(buff
                       , "%i.%i GB"
                       , (int)(size / 1073741824)
                       , (int)((size % 1073741824) / (1073741824 / 10)));
  else if(size /  1048576 > 0)
    size_msg = sprintf(buff
                       , "%i.%i MB"
                       , (int)(size / 1048576)
                       , (int)((size % 1048576) / (1048876 / 10)));
  else if(size / 1024  > 0)
    size_msg = sprintf(buff
                       , "%i.%i KB"
                       , (int)(size / 1024)
                       , (int)((size % 1024) / (1024 / 10)));
  else
    size_msg = sprintf(buff
                       , "%i bytes"
                       , (int)size);
  
  buff[size_msg] = '\0';
  return buff;
}

time_t  nfile_get_date_last_modif(const char *path)
{
  struct stat file_info;
  
  lstat(path, &file_info);
  return file_info.st_mtime;
}

char    *nfile_get_date_last_modif_s(const char *path, char *buff
                                     , int buff_size)
{
  time_t time_last;
  struct tm *gmt;
  size_t size_msg;

  buff = memset(buff, 0, buff_size);
  time_last = nfile_get_date_last_modif(path);
  gmt = gmtime(&time_last);
  size_msg = strftime(buff, buff_size, "%a, %d %b %G %H:%M:%S GMT", gmt);
  buff[size_msg] = '\0';
  return buff;
}

// return the file type
// or -1 if file not found and 0 if file type unknow
int     nfile_get_type(const char *path)
{
  struct stat file_info;

  // check if the file is valid
  if(lstat(path, &file_info))
    return(-1);
  switch((file_info.st_mode) & S_IFMT)
    {
    case S_IFREG:
      return NFILE_FT_REGFILE;
    case S_IFDIR:
      return NFILE_FT_DIRECTORY;
    case S_IFCHR:
      return NFILE_FT_CHARSPEC;
    case S_IFBLK:
      return NFILE_FT_BLOCK;
    case S_IFIFO:
      return NFILE_FT_FIFO;
    case S_IFLNK:
      return  NFILE_FT_SYMLNK;
    case S_IFSOCK:
      return NFILE_FT_SOCKET;
    }
  return 0;
}


// return 1 if the access is granted else 0,
// or -1 if lstat error
int     nfile_check_permission(const char *path
                                    , char permission)
{
  struct stat file_info;
  uid_t euid;
  gid_t egid;

  if(lstat(path, &file_info))
    return -1;

  euid = geteuid();
  egid = getegid();
  // owner permissions
  if(file_info.st_uid == euid)
    {
      if(permission == 'r' )
        {
          if((file_info.st_mode) & S_IRUSR)
            return 1;
          else
            return 0;
        }
      if(permission == 'w')
        {
          if((file_info.st_mode) & S_IWUSR)
            return 1;
          else
            return 0;
        }
      if(permission == 'x')
        {
          if((file_info.st_mode) & S_IXUSR)
            return 1;
          else
            return 0;
        }
    }
  // group permissions
  else if(file_info.st_gid == egid)
    {
      if(permission == 'r' )
        {
          if((file_info.st_mode) & S_IRGRP)
            return 1;
          else
            return 0;
        }
      if(permission == 'w')
        {
          if((file_info.st_mode) & S_IWGRP)
            return 1;
          else
            return 0;
        }
      if(permission == 'x')
        {
          if((file_info.st_mode) & S_IXGRP)
            return 1;
          else
            return 0;
        }
    }
  // other permission
  else
    {
      if(permission == 'r' )
        {
          if((file_info.st_mode) & S_IROTH)
            return 1;
          else
            return 0;
        }
      if(permission == 'w')
        {
          if((file_info.st_mode) & S_IWOTH)
            return 1;
          else
            return 0;
        }
      if(permission == 'x')
        {
          if((file_info.st_mode) & S_IXOTH)
            return 1;
          else
            return 0;
        }
    }
  return -1;
}

char    *nfile_get_mime_type(const char *path
                             , char *default_type)
{
  int   i;
  int   j;
  char  buff[8];
  char  buff2[8];
  char  *mime_types[201][2] =
    {
      {"application/SLA", "stl"},
      {"application/STEP", "step"},
      {"application/STEP", "stp"},
      {"application/acad", "dwg"},
      {"application/andrew-inset", "ez"},
      {"application/clariscad", "ccad"},
      {"application/drafting", "drw"},
      {"application/dsptype", "tsp"},
      {"application/dxf", "dxf"},
      {"application/excel", "xls"},
      {"application/i-deas", "unv"},
      {"application/java-archive", "jar"},
      {"application/mac-binhex40", "hqx"},
      {"application/mac-compactpro", "cpt"},
      {"application/vnd.ms-powerpoint", "pot"},
      {"application/vnd.ms-powerpoint", "pps"},
      {"application/vnd.ms-powerpoint", "ppt"},
      {"application/vnd.ms-powerpoint", "ppz"},
      {"application/msword", "doc"},
      {"application/octet-stream", "bin"},
      {"application/octet-stream", "class"},
      {"application/octet-stream", "dms"},
      {"application/octet-stream", "exe"},
      {"application/octet-stream", "lha"},
      {"application/octet-stream", "lzh"},
      {"application/oda", "oda"},
      {"application/ogg", "ogg"},
      {"application/ogg", "ogm"},
      {"application/pdf", "pdf"},
      {"application/pgp", "pgp"},
      {"application/postscript", "ai"},
      {"application/postscript", "eps"},
      {"application/postscript", "ps"},
      {"application/pro_eng", "prt"},
      {"application/rtf", "rtf"},
      {"application/set", "set"},
      {"application/smil", "smi"},
      {"application/smil", "smil"},
      {"application/solids", "sol"},
      {"application/vda", "vda"},
      {"application/vnd.mif", "mif"},
      {"application/vnd.ms-excel", "xlc"},
      {"application/vnd.ms-excel", "xll"},
      {"application/vnd.ms-excel", "xlm"},
      {"application/vnd.ms-excel", "xls"},
      {"application/vnd.ms-excel", "xlw"},
      {"application/vnd.rim.cod", "cod"},
      {"application/x-arj-compressed", "arj"},
      {"application/x-bcpio", "bcpio"},
      {"application/x-cdlink", "vcd"},
      {"application/x-chess-pgn", "pgn"},
      {"application/x-cpio", "cpio"},
      {"application/x-csh", "csh"},
      {"application/x-debian-package", "deb"},
      {"application/x-director", "dcr"},
      {"application/x-director", "dir"},
      {"application/x-director", "dxr"},
      {"application/x-dvi", "dvi"},
      {"application/x-freelance", "pre"},
      {"application/x-futuresplash", "spl"},
      {"application/x-gtar", "gtar"},
      {"application/x-gunzip", "gz"},
      {"application/x-gzip", "gz"},
      {"application/x-hdf", "hdf"},
      {"application/x-ipix", "ipx"},
      {"application/x-ipscript", "ips"},
      {"application/x-javascript", "js"},
      {"application/x-koan", "skd"},
      {"application/x-koan", "skm"},
      {"application/x-koan", "skp"},
      {"application/x-koan", "skt"},
      {"application/x-latex", "latex"},
      {"application/x-lisp", "lsp"},
      {"application/x-lotusscreencam", "scm"},
      {"application/x-mif", "mif"},
      {"application/x-msdos-program", "bat"},
      {"application/x-msdos-program", "com"},
      {"application/x-msdos-program", "exe"},
      {"application/x-netcdf", "cdf"},
      {"application/x-netcdf", "nc"},
      {"application/x-perl", "pl"},
      {"application/x-perl", "pm"},
      {"application/x-rar-compressed", "rar"},
      {"application/x-sh", "sh"},
      {"application/x-shar", "shar"},
      {"application/x-shockwave-flash", "swf"},
      {"application/x-stuffit", "sit"},
      {"application/x-sv4cpio", "sv4cpio"},
      {"application/x-sv4crc", "sv4crc"},
      {"application/x-tar-gz", "tar.gz"},
      {"application/x-tar-gz", "tgz"},
      {"application/x-tar", "tar"},
      {"application/x-tcl", "tcl"},
      {"application/x-tex", "tex"},
      {"application/x-texinfo", "texi"},
      {"application/x-texinfo", "texinfo"},
      {"application/x-troff-man", "man"},
      {"application/x-troff-me", "me"},
      {"application/x-troff-ms", "ms"},
      {"application/x-troff", "roff"},
      {"application/x-troff", "t"},
      {"application/x-troff", "tr"},
      {"application/x-ustar", "ustar"},
      {"application/x-wais-source", "src"},
      {"application/x-zip-compressed", "zip"},
      {"application/zip", "zip"},
      {"audio/TSP-audio", "tsi"},
      {"audio/basic", "au"},
      {"audio/basic", "snd"},
      {"audio/midi", "kar"},
      {"audio/midi", "mid"},
      {"audio/midi", "midi"},
      {"audio/mpeg", "mp2"},
      {"audio/mpeg", "mp3"},
      {"audio/mpeg", "mpga"},
      {"audio/ulaw", "au"},
      {"audio/x-aiff", "aif"},
      {"audio/x-aiff", "aifc"},
      {"audio/x-aiff", "aiff"},
      {"audio/x-mpegurl", "m3u"},
      {"audio/x-ms-wax", "wax"},
      {"audio/x-ms-wma", "wma"},
      {"audio/x-pn-realaudio-plugin", "rpm"},
      {"audio/x-pn-realaudio", "ram"},
      {"audio/x-pn-realaudio", "rm"},
      {"audio/x-realaudio", "ra"},
      {"audio/x-wav", "wav"},
      {"chemical/x-pdb", "pdb"},
      {"chemical/x-pdb", "xyz"},
      {"image/cmu-raster", "ras"},
      {"image/gif", "gif"},
      {"image/ief", "ief"},
      {"image/jpeg", "jpe"},
      {"image/jpeg", "jpeg"},
      {"image/jpeg", "jpg"},
      {"image/png", "png"},
      {"image/tiff", "tif tiff"},
      {"image/tiff", "tif"},
      {"image/tiff", "tiff"},
      {"image/x-cmu-raster", "ras"},
      {"image/x-portable-anymap", "pnm"},
      {"image/x-portable-bitmap", "pbm"},
      {"image/x-portable-graymap", "pgm"},
      {"image/x-portable-pixmap", "ppm"},
      {"image/x-rgb", "rgb"},
      {"image/x-xbitmap", "xbm"},
      {"image/x-xpixmap", "xpm"},
      {"image/x-xwindowdump", "xwd"},
      {"model/iges", "iges"},
      {"model/iges", "igs"},
      {"model/mesh", "mesh"},
      {"model/mesh", "msh"},
      {"model/mesh", "silo"},
      {"model/vrml", "vrml"},
      {"model/vrml", "wrl"},
      {"text/css", "css"},
      {"text/html", "htm"},
      {"text/html", "html"},
      {"text/plain", "asc txt"},
      {"text/plain", "asc"},
      {"text/plain", "c"},
      {"text/plain", "cc"},
      {"text/plain", "f90"},
      {"text/plain", "f"},
      {"text/plain", "h"},
      {"text/plain", "hh"},
      {"text/plain", "m"},
      {"text/plain", "txt"},
      {"text/richtext", "rtx"},
      {"text/rtf", "rtf"},
      {"text/sgml", "sgm"},
      {"text/sgml", "sgml"},
      {"text/tab-separated-values", "tsv"},
      {"text/vnd.sun.j2me.app-descriptor", "jad"},
      {"text/x-setext", "etx"},
      {"text/xml", "xml"},
      {"video/dl", "dl"},
      {"video/fli", "fli"},
      {"video/flv", "flv"},
      {"video/gl", "gl"},
      {"video/mpeg", "mp2"},
      {"video/mp4", "mp4"},
      {"video/mpeg", "mpe"},
      {"video/mpeg", "mpeg"},
      {"video/mpeg", "mpg"},
      {"video/quicktime", "mov"},
      {"video/quicktime", "qt"},
      {"video/vnd.vivo", "viv"},
      {"video/vnd.vivo", "vivo"},
      {"video/x-fli", "fli"},
      {"video/x-ms-asf", "asf"},
      {"video/x-ms-asx", "asx"},
      {"video/x-ms-wmv", "wmv"},
      {"video/x-ms-wmx", "wmx"},
      {"video/x-ms-wvx", "wvx"},
      {"video/x-msvideo", "avi"},
      {"video/x-sgi-movie", "movie"},
      {"www/mime", "mime"},
      {"x-conference/x-cooltalk", "ice"},
      {"x-world/x-vrml", "vrm"},
      {"x-world/x-vrml", "vrml"}
    };

  for(i = strlen(path) - 1, j = 0
        ; path[i] != '.' && i > 0 && j < 8
        ; i--, j++)
    buff[j] = path[i];
  
  if(i == 0 || path[i] != '.')
    return(default_type);
    
  buff[j] = '\0';
  buff2[j] = '\0';
  for(i = 0, j -= 1; buff2[i] && j >= 0; i++, j--)
    buff2[i] = buff[j];
  for(i = 0; i < 200; i++)
    if(!strcasecmp(buff2, mime_types[i][1]))
      return(mime_types[i][0]);
  return(default_type);
}

